//#define __FORCE_TRACE__
//#define __FORCE_DEBUG__
//#define __FORCE_INFO__
#define THIS_FILE "tools\fn_createUnit.sqf"
#include <tmf_constants.h>
#include <tmf_macros.h>

TRACE_1("Start",_this);
TMFPROFILERSTART;

private ["_unit","_units","_grp","_pos","_type","_init","_rank"];
DEFAULT_PARAM(_type,0,"");
DEFAULT_PARAM(_grp,1,grpNull);
DEFAULT_PARAM(_pos,2,ARR_3(0,0,0));
DEFAULT_PARAM(_dist,3,0);
DEFAULT_PARAM(_init,4,"");
DEFAULT_PARAM(_rank,5,"private");
DEFAULT_PARAM(_skill,6,(call FFUNC(getUnitSkill)));

_new_pos = [];
if (_dist > 0) then {
	_new_pos = _pos findEmptyPosition[ 1 , _dist , _type];
	if (count _new_pos > 0) then { _pos = _new_pos; };
};

_unit = objNull;
_units = units _grp;
if (isnil "_units") then { _units = []; };			
TRACE_2("CreateUnit",_type,ARR_5(_pos, _grp, _init, _skill, _rank));
_type createUnit [ _pos, _grp, _init, _skill, _rank];
{ if (!(_x in _units)) exitWith{_unit = _x;};
} foreach (units _grp);

TRACE_1("End",ARR_3(_unit,typeof _unit,getPos _unit));
TMFPROFILERSTOP;
_unit
